package com.ruoyi.wms.service.impl;

import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.wms.domain.WmsStockInDetails;
import com.ruoyi.wms.mapper.WmsStockInMapper;
import com.ruoyi.wms.domain.WmsStockIn;
import com.ruoyi.wms.service.IWmsStockInService;

import javax.annotation.Resource;

/**
 * 入库管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-11-07
 */
@Service
public class WmsStockInServiceImpl implements IWmsStockInService 
{
    @Resource
    private WmsStockInMapper wmsStockInMapper;

    /**
     * 查询入库管理
     * 
     * @param stockInUuid 入库管理主键
     * @return 入库管理
     */
    @Override
    public WmsStockIn selectWmsStockInByStockInUuid(String stockInUuid)
    {
        return wmsStockInMapper.selectWmsStockInByStockInUuid(stockInUuid);
    }

    /**
     * 查询入库管理列表
     * 
     * @param wmsStockIn 入库管理
     * @return 入库管理
     */
    @Override
    public List<WmsStockIn> selectWmsStockInList(WmsStockIn wmsStockIn)
    {
        return wmsStockInMapper.selectWmsStockInList(wmsStockIn);
    }

    /**
     * 新增入库管理
     * 
     * @param wmsStockIn 入库管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWmsStockIn(WmsStockIn wmsStockIn)
    {
        wmsStockIn.setCreateBy(SecurityUtils.getUsername());
        wmsStockIn.setCreateTime(DateUtils.getNowDate());
        int rows = wmsStockInMapper.insertWmsStockIn(wmsStockIn);
        insertWmsStockInDetails(wmsStockIn);
        return rows;
    }

    /**
     * 修改入库管理
     * 
     * @param wmsStockIn 入库管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWmsStockIn(WmsStockIn wmsStockIn)
    {
        wmsStockIn.setUpdateBy(SecurityUtils.getUsername());
        wmsStockIn.setUpdateTime(DateUtils.getNowDate());
        wmsStockInMapper.deleteWmsStockInDetailsByFkStockInUuid(wmsStockIn.getStockInUuid());
        insertWmsStockInDetails(wmsStockIn);
        return wmsStockInMapper.updateWmsStockIn(wmsStockIn);
    }

    /**
     * 批量删除入库管理
     * 
     * @param stockInUuids 需要删除的入库管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsStockInByStockInUuids(String[] stockInUuids)
    {
        wmsStockInMapper.deleteWmsStockInDetailsByFkStockInUuids(stockInUuids);
        return wmsStockInMapper.deleteWmsStockInByStockInUuids(stockInUuids);
    }

    /**
     * 删除入库管理信息
     * 
     * @param stockInUuid 入库管理主键
     * @return 结果
     */
    @Override
    public int deleteWmsStockInByStockInUuid(String stockInUuid)
    {
        wmsStockInMapper.deleteWmsStockInDetailsByFkStockInUuid(stockInUuid);
        return wmsStockInMapper.deleteWmsStockInByStockInUuid(stockInUuid);
    }

    /**
     * 新增入库单明细信息
     * 
     * @param wmsStockIn 入库管理对象
     */
    public void insertWmsStockInDetails(WmsStockIn wmsStockIn)
    {
        List<WmsStockInDetails> wmsStockInDetailsList = wmsStockIn.getWmsStockInDetailsList();
        String stockInUuid = wmsStockIn.getStockInUuid();
        if (StringUtils.isNotNull(wmsStockInDetailsList))
        {
            List<WmsStockInDetails> list = new ArrayList<WmsStockInDetails>();
            for (WmsStockInDetails wmsStockInDetails : wmsStockInDetailsList)
            {
                wmsStockInDetails.setFkStockInUuid(stockInUuid);
                list.add(wmsStockInDetails);
            }
            if (list.size() > 0)
            {
                wmsStockInMapper.batchWmsStockInDetails(list);
            }
        }
    }
}
